﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.MassLine;

namespace ZHDJ.BranchCenter.Module.MassLine
{
    public partial class DocumentList : PageBase
    {
        #region 属性
        /// <summary>
        /// DataSource
        /// </summary>
        public DataSet DataSource
        {
            get
            {
                return PartyArticle.GetMassLineSuperiorSpirit(Grid1.PageIndex, Grid1.PageSize, this.txtTitle.Text, this.Module, this.Category);
            }
        }

        public string Module
        {
            get
            {
                return this.Request["module"] ?? "MassLine";
            }
        }

        /// <summary>
        /// SuperiorsSpirit  上级精神
        /// PeopleDeeds    典型事迹 
        /// </summary>
        private string Category
        {
            get
            {
                return this.Request["category"] ?? "SuperiorsSpirit";
            }
        }
        #endregion

        #region Page_Load
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindData();
                this.LoadData();
            }
        }
        #endregion

        #region 数据访问
        /// <summary>
        /// BindData
        /// </summary>
        protected void BindData()
        {
            this.hdCategory.Value = this.Category;
        }

        /// <summary>
        /// LoadData
        /// </summary>
        protected void LoadData()
        {
            btnAdd.OnClientClick = Dialog.GetShowReference("DocumentEdit.aspx", "新增上级精神");
            this.Grid1.DataSource = this.DataSource;
            this.Grid1.DataBind();
        }

        /// <summary>
        /// 获取地址
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string GetURL(object obj)
        {
            return "DocumentEdit.aspx?DocumentID=" + obj.ToString() + "&Category=" + this.Category;
        }

        private void BindGrid()
        {
            var ds = this.DataSource;

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("PartyEduHonourEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("PartyEduHonourDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string IDs = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PartyEduHonourDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}

                var del = DB.PartyArticles.FirstOrDefault(t => t.ID == IDs);
                DB.PartyArticles.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }
        #endregion

        #region 按钮事件
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyArticles.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyArticles.Remove(u));
            DB.SaveChanges();
            BindGrid();
        }

        #endregion
        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}